package com.pysoft.safe.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pysoft.common.util.Identities;
import com.pysoft.safe.dao.entity.RoleEntity;
import com.pysoft.safe.dao.mapper.RoleMapper;
import com.pysoft.safe.dao.mapper.RoleResourceMapper;
import com.pysoft.safe.service.RoleService;

public class RoleServiceImpl implements RoleService {

	private RoleMapper roleDao;
	private RoleResourceMapper roleResourceDao;

	@Override
	public void removeRole(String id) {
		this.getRoleDao().delete(id);
	}

	@Override
	public List<RoleEntity> query(Map<String, Object> paramMap) {
		return this.getRoleDao().query(paramMap);
	}

	@Override
	public RoleEntity getRole(String id) {
		return this.getRoleDao().get(id);
	}

	@Override
	public RoleEntity addRole(String pid) {
		RoleEntity entity = new RoleEntity();
		String id = Identities.uuid2();
		entity.setId(id);
		entity.setPid(pid);
		entity.setName(id);
		this.getRoleDao().insert(entity);
		return entity;
	}

	@Override
	public void modifyRole(RoleEntity entity) {
		this.getRoleDao().update(entity);
	}

	@Override
	public void relatingResource(String roleId, List<String> resourceIdList) {
		roleResourceDao.deleteByRole(roleId);
		Map<String, Object> entity = null;
		for(String resourceId:resourceIdList) {
			entity = new HashMap<String, Object>();
			entity.put("roleId", roleId);
			entity.put("resourceId", resourceId);
			roleResourceDao.insert(entity);
		}
	}
	
	@Override
	public void cancelRelatingResource(String roleId) {
		roleResourceDao.deleteByRole(roleId);
	}
	
	@Override
	public List<String> getRelatingResources(String roleId) {
		return roleResourceDao.queryResource(roleId);
	}
	
	public RoleMapper getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleMapper roleDao) {
		this.roleDao = roleDao;
	}

	public RoleResourceMapper getRoleResourceDao() {
		return roleResourceDao;
	}

	public void setRoleResourceDao(RoleResourceMapper roleResourceDao) {
		this.roleResourceDao = roleResourceDao;
	}
}
